home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / basic / basctut.com / GWBT01.TXT < prev   
Encoding:
Text File  |  1990-06-10  |  17.7 KB  |  372 lines

  1. WELCOME!
  2.  
  3. This file (GWBT01.TXT) is the first in what I hope will be a long and 
  4. enjoyable series of 'programming classes' that will aid you in learning some 
  5. of the beginner and intermediate level features of the BASIC programming 
  6. language.
  7.  
  8. First, let me introduce myself.  I am Richard Harper, professionally a 
  9. paramedic in northwest Michigan, but for the last 16 years I have been 
  10. involved with, and interested in, computers and programming.  I have a few 
  11. Shareware utilities that I currently am marketing, both written entirely in 
  12. the QuickBASIC programming language.
  13.  
  14. Please keep in mind that the generic term 'BASIC language' can actually refer 
  15. to one of several flavors.  IBM PC and XT users are familiar with the 'BASIC' 
  16. and 'BASICA' in their ROM's.  Users of 'compatible' systems that use MS-DOS 
  17. are familiar with the GWBASIC.EXE form of Basic.  Others use the Professional 
  18. BASIC and QuickBASIC languages from Microsoft, while some use the TurboBASIC 
  19. package from Borland.  When I refer to 'BASIC' in this series, I will be 
  20. indicating either BASICA or GWBASIC, unless noted otherwise.
  21.  
  22. The examples presented in this series will require the use of either the 
  23. BASICA or GWBASIC packages.  For those following the series using either 
  24. QuickBASIC, Professional BASIC or TurboBASIC, please refer to the manuals that 
  25. came with your software for any specific changes that might need to be made in 
  26. the programs as presented here to fit them to your system.
  27.  
  28. Each tutorial is intended to serve as a stand-alone module.  Although we will 
  29. be building on each tutorial as we go, you should be able to pick up any one, 
  30. in any order, and learn from it.  We will start by discussing one (or more 
  31. related) commmand(s) from BASIC, showing some simple programs that demonstrate 
  32. the topic under discussion, then end the tutorial with a 'homework assignment' 
  33. that will lead you in the direction of next month's tutorial topic.
  34.  
  35. Of course, you might have some questions regarding the material presented.  
  36. If so, I would encourage you to post them in the IBM New Users/Fun Forum (GO 
  37. IBMNEW) on CompuServe, in Message area #0 (General/New Uploads) either to me 
  38. directly (Richard Harper, User ID 76670,110) or in a message to ALL.  If you 
  39. prefer, you could send messages by CompuServe Mail, but then no one else will 
  40. 'hear' your question, nor the answer.  I will be checking for questions, 
  41. comments or other input on at least a weekly basis (usually on Wednesdays
  42. and/or Saturdays) to try to keep answers coming to you on a timely basis.
  43. By posting public messages, we can closely approach a 'classroom' type 
  44. enviromnent where everyone can participate in questions and discussions on the 
  45. topics of each lesson using the message areas. 
  46.  
  47. Right now, you might be asking yourself why you should bother to learn 
  48. programming at all.  'Hey, hasn't someone somewhere already written a program 
  49. that works better than the ones I might write?', you might ask...
  50.  
  51. The answer is:  Probably NOT!  With a reasonable grasp of the BASIC language, 
  52. it is easy to write many programs that perform relatively complex tasks, and 
  53. the program YOU write will be expressely tailored to YOUR preferences.  Why 
  54. settle for a choice from a menu, when you can cook it yourself?
  55.  
  56. Computer programming using BASIC is not difficult at all.  In fact, if you can 
  57. speak English you already know at least half of the BASIC language, and this 
  58. half includes the most useful 90% of the commands available from BASIC.
  59.  
  60. Ready?  Let's dig into our first topic:
  61.  
  62. *******************************************************************************
  63. GW-BASIC TUTORIAL PART ONE:  VARIABLES, INPUT AND OUTPUT
  64. *******************************************************************************
  65.  
  66. In order to write a 'useful' program, we need to examine ways to do three 
  67. things.
  68.  
  69. First, we need a way for the user to enter some sort of information to the 
  70. program.  That information might be a name, number, or some other type of 
  71. information that the program needs to operate.
  72.  
  73. Second, the computer will usually process this information in some manner.  We 
  74. might add a series of numbers, add to the name provided by a user, or so on.
  75.  
  76. Finally, we need some way of showing this information (after processing it) to 
  77. the user.
  78.  
  79. Fortunately, BASIC provides very simple ways of doing the inputting and 
  80. outputting of information, and a number of ways to store and process the 
  81. information.
  82.  
  83. Before we can ask for information, process it, or show it to the user, we need 
  84. to understand how BASIC stores such information.  The BASIC language provides 
  85. what are called 'variables' to obtain, store and process information.
  86.  
  87. A variable is simply a name that you give to BASIC and tell it what type of 
  88. information is being stored.  The rules for naming variables are very few and 
  89. quite simple:
  90.  
  91. 1)  BASIC variables must start with an alphabetic letter (A-Z).
  92.  
  93. 2)  BASIC variables can consist of any combination of letters (A-Z) or
  94.     numbers (0-9) following the initial letter, but only the first 40
  95.     characters are recognized.  Punctuation marks are not allowed.
  96.  
  97. 3)  You cannot use BASIC keywords (commands) as a name for a variable,
  98.     as this would confuse BASIC as to whether the variable is a varible
  99.     or a command!
  100.  
  101. 4)  Some variables will require an optional type identifier as the last
  102.     character, as follows:
  103.  
  104.         Integer numeric variables               %
  105.         Single precision numeric variables      !
  106.         Double precision numeric variables      #
  107.         String (character) variables            $
  108.  
  109. Don't get TOO upset about the four types listed above, as our introductory 
  110. sessions will deal with only two of the above:  the single precision and 
  111. string variable types.
  112.  
  113. STRING VARIABLES are used to hold data that you want to represent strictly as 
  114. the exact characters typed in.  A string variable is identified by a dollar 
  115. sign ($) at the end of the variable name.  In addition, if a string variable 
  116. is given a value within the program, it MUST have quote marks around it.
  117.  
  118. SINGLE PRECISION VARIABLES are used to hold data that is numeric in nature.  
  119. You MUST use a numeric variable if you want to perform arithmetic operations 
  120. on the data, such as addition, multiplication, etc.
  121.  
  122. To show the difference between the different variable types, start up BASIC 
  123. (if you aren't sure how to start BASIC on your system, check your user 
  124. manuals and/or the BASIC manual that came with your computer for details on 
  125. how to start and exit from BASIC) and type in the following lines as shown:
  126.  
  127. QUANTITY$ = "3"
  128. PRICE$ = "21.45"
  129. PRINT QUANTITY * PRICE
  130.  
  131. What happened?  The first two lines seemed OK (in fact, BASIC probably said 
  132. 'OK' after them), but on the third line, BASIC said that this was a 'Type 
  133. mismatch' error!  This is because you cannot multiply string variables.
  134.  
  135. Try this instead (type it in after you've tried the first three lines):
  136.  
  137. PRINT PRICE + QUANTITY
  138.  
  139. What do you see?  You always thought 3 + 21.45 was 24.45, right?  Not with 
  140. strings!  BASIC interprets the strings exactly as typed in, and in adding
  141. them simply combines one string after the other.
  142.  
  143. Now try this:
  144.  
  145. QUANTITY = 3
  146. PRICE = 21.45
  147. PRINT QUANTITY * PRICE
  148. PRINT PRICE + QUANTITY
  149.  
  150. Well, it worked all right this time, exactly as you would expect.
  151.  
  152. Unless you add one of the variable type characters ($ # % !) at the end of a 
  153. variable name, BASIC assumes it is a single-precision numeric variable.  As we 
  154. are using the default single-precision numeric variable types, you need only 
  155. remember for now these two rules:
  156.  
  157. 1)  If a variable will contain alphabetic characters, or is to be interpreted 
  158. as exactly what the user typed in, or needs to have no arithmetic done on it, 
  159. add a dollar sign ($) to the end and make it a STRING VARIABLE;
  160.  
  161. 2)  If a variable is to be used in arithmetic, or needs to have a number 
  162. stored in it, put no sign on the end and it is a SINGLE PRECISION VARIABLE.
  163.  
  164. I suggest that you choose names that describe the value being stored, so that 
  165. you (or others) can later tell what the variable was intended to store.  For 
  166. example:
  167.  
  168. A variable to store an Employee ID number might be called:  EMPLOYEEID
  169.  
  170. A variable to store a first name might be called:           FIRSTNAME$
  171.  
  172. A variable to store an item count might be called:          COUNT
  173.  
  174. Enough on variables for now.  Let's dig in and write a program!
  175.  
  176. Assume you want to ask a user for his/her name, then print a nice little 
  177. welcome message.  In analyzing the program, we see we'll need to use three 
  178. steps in it:
  179.  
  180.                       Ask the user for his/her name
  181.                                     |
  182.                                     V
  183.                 Add the user's name to a welcome message
  184.                                     |
  185.                                     V
  186.                  Show the welcome message on the screen
  187.  
  188. The above diagram is a 'flowchart', and shows the direction that the program 
  189. will be moving in.  It doesn't hurt to get into the practice of making up 
  190. flowcharts to help you understand how a program will operate, and they can 
  191. provide valuable reminders later when you need to get bugs out of your own 
  192. programs.
  193.  
  194. To ask the user for his/her name, we will use the INPUT statement from BASIC.  
  195. INPUT prints a question mark on the screen, then pauses while the user puts in 
  196. the desired information, pressing the Enter key to signal that all the needed 
  197. information has been provided.  In its simplest form, it is used as:
  198.  
  199.                 INPUT variable-name
  200.  
  201. where variable-name is the type of variable to store the input.  You must 
  202. determine whether the input is to be a string variable (characters) or numeric 
  203. variable (numbers), and use that type of variable for the input value.
  204.  
  205. You can also use input to provide some information for the user prior to 
  206. getting the variable.  That form of the INPUT statement is:
  207.  
  208.                 INPUT "text to be printed",variable-name
  209.  
  210. Note that the "text to be printed" can be anything you can put between 
  211. quotation marks.  For example, in our first step of our program, we want the 
  212. user's name, so it would be logical to ask the user what his/her name is.  We 
  213. would therfore use the statement:
  214.  
  215.                 INPUT "What is your name, please"; NAME$
  216.  
  217. You see that I have used a variable called NAME$ to store the name.  NAME is a 
  218. pretty descriptive word, indicating that a name is stored here, and the dollar 
  219. sign ($) at the end indicates that it is a string variable (holds character-
  220. based data as opposed to numbers).  You could just as well use the variables 
  221. USERNAME$, or NAMEOFUSER$, or any other variable name that will remind you 
  222. what is being stored there.
  223.  
  224. Next, we wanted to add this name to a welcome message to the user.  Let's 
  225. assume that we want to print "Hello there,", followed by a space, then by the 
  226. user's name.  BASIC will allow you to combine (concanate) strings by putting 
  227. them in the desired order, and putting a plus sign (+) between them.  So, we 
  228. could create the welcome message in BASIC with the statement:
  229.  
  230.                 WELCOME$ = "Hello there, " + NAME$
  231.  
  232. Note that there is a space following the comma in the welcome message.  This 
  233. is necessary if a space is desired between the welcome message and the name, 
  234. as BASIC isn't smart enough to put one there unless you do!
  235.  
  236. Also, note that the variable name (WELCOME$) indicates a string variable, and 
  237. that the name is pretty descriptive of what the varible is storing.
  238.  
  239. Finally, we want to print the welcome message on the screen.  This is done in 
  240. BASIC by using the PRINT statement.  The simplest form (and the one we'll 
  241. start out with" is the form:
  242.  
  243.                 PRINT variable-name
  244.  
  245. where variable-name is the variable we want to print.  So, our command to 
  246. print the welcome message would be:
  247.  
  248.                 PRINT WELCOME$
  249.  
  250. Finally, we put the whole program together:
  251.  
  252. 10 ' WELCOME.BAS - ask the user to input his/her name, then print it on the
  253. 20 ' screen with a nice little welcome message.
  254. 30 ' Program from GWBT01.TXT BASIC tutorial file 06/15/1990
  255. 40 ' Written by Richard Harper and customized by (your name here!)
  256. 50 '
  257. 60 ' Start of main program body:
  258. 70 INPUT "What is your name, please"; NAME$
  259. 80 WELCOME$ = "Hello there, " + NAME$
  260. 90 PRINT WELCOME$
  261. 100 END
  262. 110 ' End of program - WELCOME.BAS
  263.  
  264. You'll note that I added many comment lines (the lines where the line number 
  265. is followed by a single quote (') mark) to the program.  This is a good idea 
  266. to do for most programs, as you can give yourself reminders about the program, 
  267. what it does, and why you wrote it.  You can put any information you want into 
  268. the comment lines, as BASIC will ignore them when the program is run.  BASIC 
  269. refers to these as REM or REMARK lines.
  270.  
  271. Also note that the program ends with a line that says END.  This is also a 
  272. good habit to get into doing, as programs we write later may contain parts 
  273. following the actual end that only get run in case of unexpected errors (these 
  274. are called ERROR TRAPPING ROUTINES), or routines that the main program calls 
  275. to do special tasks (these are called SUBROUTINES).
  276.  
  277. Go ahead and enter this program, and try it out.  Whatever you type in as the 
  278. name will be displayed in the welcome message, with only one exception.  
  279. Most versions of BASIC (including ours) allow you to ask the user for several 
  280. input values at once, and these are expected to be seperated by commas (,) so 
  281. that BASIC can tell which parts go where.  As an example:
  282.  
  283.                 INPUT "What is your name and age", NAME$, AGE
  284.  
  285. would allow you to enter the answer as:
  286.  
  287.                 Richard Harper,31 (press Enter)
  288.  
  289. and answer both variables at once.  BASIC would make Richard Harper one 
  290. value, and 31 another.  It would then look at the INPUT statement, see that 
  291. Richard Harper is a valid string input, that 31 is a valid numeric input, and 
  292. put the values in the appropriate variables.  If you put a comma in the middle 
  293. of a single value INPUT statement (like our line 70 above), BASIC tries to 
  294. split it into two parts.  Say you answered the name question as follows: 
  295.  
  296.                 Harper, Richard
  297.  
  298. BASIC would then make Harper one input value, and Richard a second, as they 
  299. are seperated by commas.  It would then see that we ask for only one value 
  300. (the NAME$ variable), and it would rudely tell you: 
  301.  
  302.                 ?Redo from start
  303.  
  304. and simply print another question mark.  There are ways around this, and we 
  305. will deal with them later.  For now, just avoid the use of commas when putting 
  306. in string information.
  307.                    
  308. And we've written our first program!  Not as hard as it looked.  Try changing 
  309. around some of the program and see how YOU can customize it to do EXACTLY what 
  310. you want it to.  This is your first taste of the power of BASIC.
  311.  
  312. *******************************************************************************
  313. HOMEWORK ASSIGNMENT:
  314. *******************************************************************************
  315.  
  316. So far, we've seen how to get information from the user, process it, and print 
  317. it on the screen.  For next month's assignment (07/15/1990), we're going to 
  318. explore some more involved uses of INPUT (LINE INPUT) and PRINT (LOCATE and 
  319. PRINT USING) to allow the entry of more detailed information, and some ways to 
  320. make our screen output look a little nicer.
  321.  
  322. Remember:  If you have any questions or comments, please feel free to post 
  323. them in the IBMNEW Message Area #0 addressed to me.  Also don't forget that 
  324. there are almost as many ways of writing a program as there are programmers!  
  325.  
  326. The material presented in these tutorials is only one way of many to solve the 
  327. discussion problems.  If you can come up with a better, or easier, or faster 
  328. way of solving a problem, share it with the rest of us!
  329.  
  330. Next month's tutorial (appearing on or around 07/15/1990, and titled 
  331. GWBT02.TXT for GW-Basic Tutorial number 2) will include the above discussion 
  332. topics, along with selected questions or comments from you!  Participate and 
  333. make this a great series!  I'll be waiting to hear from everyone as we explore 
  334. the world of BASIC programming. 
  335.  
  336. <end of file: GWBT01.TXT>
  337.  
  338.          ----------------end-of-author's-documentation---------------
  339.  
  340.                         Software Library Information:
  341.  
  342.                    This disk copy provided as a service of
  343.  
  344.                         The Public (Software) Library
  345.  
  346.          We are not the authors of this program, nor are we associated
  347.          with the author in any way other than as a distributor of the
  348.          program in accordance with the author's terms of distribution.
  349.  
  350.          Please direct shareware payments and specific questions about
  351.          this program to the author of the program, whose name appears
  352.          elsewhere in  this documentation. If you have trouble getting
  353.          in touch with the author,  we will do whatever we can to help
  354.          you with your questions. All programs have been tested and do
  355.          run.  To report problems,  please use the form that is in the
  356.          file PROBLEM.DOC on many of our disks or in other written for-
  357.          mat with screen printouts, if possible.  The P(s)L cannot de-
  358.          bug programs over the telephone.
  359.  
  360.          Disks in the P(s)L are updated monthly, so if you did not get
  361.          this disk  directly from the P(s)L,  you should be aware that
  362.          the files in this set may no  longer be the current versions.
  363.  
  364.          For a copy of the latest monthly software library newsletter
  365.          and a list of the 2,000+ disks in the library, call or write
  366.  
  367.                         The Public (Software) Library
  368.                               P.O.Box 35705
  369.                            Houston, TX 77235-5705
  370.                                (713) 524-6394
  371.  
  372.